﻿package com.sp.pay.common;

import java.text.SimpleDateFormat;

public class Tool {


	/**
	  * 截取字符串
	  * @param str 原字符串
	  * @param begin 开始位置
	  * @param length 截取字符长度
	  * @return
	  */
	public static String subString(String str,int begin,int length)
	{
		String s;
		if(str==null||str.trim().equals(""))
		{
			s="";
		}else
		{
			StringBuffer a=new StringBuffer(str);
			if(a.length()>(begin+length))
			{
				s=a.substring(begin,begin+length);
			}
			else if(a.length()>begin)
			{
				s=a.substring(begin,a.length());
			}
			else
			{
				s=a.toString();
			}
		}
		return s;
	}
	/**
	   * 将字符串转化为整型数字
	   * @param str
	   * @return
	   */
	public static int getInt(String str){ 
		if(str!=null&&!str.equals(""))
		{
			str=str.trim();
			str=str.replace("+","");
			String ers="[+-]*[0-9]+";
			if(str.matches(ers))
			{
				return Integer.parseInt(str);
			}else
			{
				return 0;
			}
		}else
		{
			return 0;
		}
	}
	/**
	 * 获得time相隔num天的时间
	 * @param time 不和条件为当天
	 * @param num 天数 大于0 加 小于0减的天数
	 * @return yyyy-mm-dd
	 */
	public static String getTime(String time,int num){
		
		long t1=0;
		long t2=0;
		java.util.Date d1=new java.util.Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String str=sf.format(new java.util.Date ().getTime());
		try{
			if(time==null||time.equals(""))
			{
				//tim=2009-06-11 t1=1244649600000 tim2=2009-06-10
				time=sf.format(new java.util.Date ().getTime());
				//d1=sf.parse(time);
				t1=d1.getTime();
				t2=(t1/1000+num*24*3600)*1000;
				str=sf.format(t2);
				
			}else if(time.trim().length()==10)
			{
				
				d1=sf.parse(time);
				t1=d1.getTime();
				t2=(t1/1000+num*24*3600)*1000;
				str=sf.format(t2);
			}else
			{
				d1=sf2.parse(time);
				t1=d1.getTime();
				t2=(t1/1000+num*24*3600)*1000;
				str=sf2.format(t2);
			}
			
			//logger.info("time= " +time+ " t1= " + t1 +" tim2= "+str);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	  return str.trim();
	}
	 /**
	  * 获得两个数之间的随机数(包括 begin,end)
	  * @param begin
	  * @param end
	  * @return
	  */
	public static int getRandom(int begin,int end)
	{
		int m=0;
		end=end+1;
		if(end<begin)
		{
			m=0;
		}else if(end==begin)
		{
			m=begin;
		}else
		{
			java.util. Random random = new java.util. Random();
	 		m=random.nextInt(end-begin)+begin;
		}
		return m;
	}
 public static void main(String[] args){
		 
		 //System.out.println("safdafd = " + subString("safdafd" ,10,4) +"cnt.indexOf(8322ssfffs)"+"8322sdsdf".indexOf("8322"));
		 
		// System.out.println("safdafd = " + subString("safdafd" ,2,4));
		 
		// System.out.println("safddd = " + subString("safddd" ,2,4));
		// System.out.println("safdd = " + subString("safdd" ,2,4));
		 
		/* String  strNewWhiteText="13456880894$0000018502$8xij$41091864$280$1$1";
		 String note[]=strNewWhiteText.split("\\$");
		 
		 for(int i=0;i<note.length;i++)
			{
				System.out.println("num["+i+"] = " +note[i]);
			}*/
	 	String time=new SimpleDateFormat("HH:mm:ss").format(new java.util.Date ().getTime());
	 	System.out.println("time= "+ time);
	 	time="00:23:23";
		System.out.println("00:00:01".compareTo(time));
		System.out.println("07:00:01".compareTo(time));
	 }


}
